package org.itboys.mobile.service.mongo.site;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.JoinPicture;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.service.mongo.web.SundayMemberRecordService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/9/28
 * 描述：雄安加盟网_图库
 */
@Service
public class JoinSitePictureService extends BaseMongoService<JoinPicture>{
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Autowired
    private SundaySiteMemberRecordService memberRecordService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<JoinPicture> getEntityClass() {
        return JoinPicture.class;
    }

    /**
     * 查询
     * @param request
     * @param memberRecordId
     * @return
     */
    public List<JoinPicture> select(HttpServletRequest request,Long memberRecordId,Integer type){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("memberRecordId",memberRecordId);
        if(type!=null&&type!=0){
            param.put("type",type);
        }
        return super.list(param);
    }

    /**
     * 新增图片   更新用户记录状态
     * @param memberRecordId
     * @param type
     * @param title
     * @param url
     */
    public void savePicture(Long memberRecordId,Integer type,String title,String url){

        JoinPicture picture=new JoinPicture(memberRecordId,type,0,0,title,url,1);
        SundayMemberRecord memberRecord=memberRecordService.getById(memberRecordId);
        memberRecord.setStatus(1);
        memberRecordService.updateExceptEmpty(memberRecord.getId(),memberRecord);
        super.save(picture);
    }

    /**
     * 获取轮播图
     * @param request
     * @param memberRecordId
     * @return
     */
    public List<JoinPicture> selectRoll(HttpServletRequest request,Long memberRecordId){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("memberRecordId",memberRecordId);
        param.put("status",2);
        param.put("isRoll",1);
        return super.list(param,6);
    }

    /**
     * 获取背景图
     * @param request
     * @param memberRecordId
     * @return
     */
    public JoinPicture selectBack(HttpServletRequest request,Long memberRecordId){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("memberRecordId",memberRecordId);
        param.put("status",2);
        param.put("isBack",1);
        List<JoinPicture> pictures=super.list(param);
        if(ListUtil.isNotNull(pictures)){
            return pictures.get(0);
        }else {
            return null;
        }

    }
    /**
     * 设置是否为轮播图和是否为背景图
     * @param pictureId
     * @param isBack
     * @param isRoll
     */
    public void setStatus(Long pictureId,Integer isBack,Integer isRoll){
        JoinPicture picture=super.getById(pictureId);
        if(isBack!=null){
            picture.setIsBack(isBack);
        }
        if(isRoll!=null){
            picture.setIsRoll(isRoll);
        }
        super.updateExceptEmpty(pictureId,picture);

    }
    /**
     * 设置图片类型
     * @param pictureId
     * @param type
     */
    public void setType(Long pictureId,Integer type){
        JoinPicture picture=super.getById(pictureId);
        picture.setType(type);
        super.updateExceptEmpty(pictureId,picture);
    }
}
